﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class BalanceCenter_ScoreVirtual : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            long id = ShoveEL._Convert.StrToLong(this.Request["id"], -1);

            if ((_User.Competences[Competences.Administrator]) && (id >= 0))
            {
                tbID.Text = id.ToString();
            }
            else
            {
                tbID.Text = _User.ID.ToString();
            }

            BindDataForYearMonth();
            BindData();
            BindData2();
        }
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        SecondLevelPassword = true;
        RequestLoginPage = "BalanceCenter/Score.aspx";

        base.OnInit(e);
    }

    #endregion

    private void BindDataForYearMonth()
    {
        ddlYear.Items.Clear();
        DateTime dt = DateTime.Now;
        int Year = dt.Year;
        int Month = dt.Month;

        if (Year < 2006)
        {
            btnGO.Enabled = false;
            return;
        }

        for (int i = 2007; i <= Year; i++)
        {
            ddlYear.Items.Add(new ListItem(i.ToString() + "年", i.ToString()));
        }

        ddlYear.SelectedIndex = ddlYear.Items.Count - 1;

        ddlMonth.SelectedIndex = Month - 1;
    }

    private void BindData()
    {
        long ID = ShoveEL._Convert.StrToLong(tbID.Text, -1);
        if (ID < 0)
        {
            ID = _User.ID;
        }

        DataSet ds = DAL.Procedures.P_GetUserAccountDetail2.Open(ID, int.Parse(ddlYear.SelectedValue), int.Parse(ddlMonth.SelectedValue));

        if (ds == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        if (ds.Tables.Count == 0)
        {
            PublicFunction.GoError(ErrorNumber.NoData, "暂无数据", this.GetType().BaseType.FullName);

            return;
        }

        DataTable dt = ds.Tables[0];

        g.DataSource = dt;
        g.DataBind();
    }

    protected void g_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
        {
            for (int i = 3; i <= 7; i++)
            {
                double money = ShoveEL._Convert.StrToDouble(e.Item.Cells[i].Text, 0);
                e.Item.Cells[i].Text = (money == 0) ? "" : money.ToString("N");
            }

            double money3 = ShoveEL._Convert.StrToDouble(e.Item.Cells[3].Text, 0) + ShoveEL._Convert.StrToDouble(e.Item.Cells[4].Text, 0) + ShoveEL._Convert.StrToDouble(e.Item.Cells[5].Text, 0);
            double money4 = ShoveEL._Convert.StrToDouble(e.Item.Cells[6].Text, 0) + ShoveEL._Convert.StrToDouble(e.Item.Cells[7].Text, 0);
            e.Item.Cells[8].Text = (money3 - money4).ToString("N");

            e.Item.Attributes["onmouseover"] = "this.name=this.style.backgroundColor;this.style.backgroundColor='MistyRose'";
            e.Item.Attributes["onmouseout"] = "this.style.backgroundColor=this.name;";
        }
    }

    private void BindData2()
    {
        long ID = ShoveEL._Convert.StrToLong(tbID.Text, -1);
        if (ID < 0)
        {
            ID = _User.ID;
        }

        DataSet ds = DAL.Procedures.P_GetUserVirtualDetail.Open(ID, int.Parse(ddlYear.SelectedValue), int.Parse(ddlMonth.SelectedValue));

        if (ds == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        if (ds.Tables.Count == 0)
        {
            PublicFunction.GoError(ErrorNumber.NoData, "暂无数据", this.GetType().BaseType.FullName);

            return;
        }

        DataTable dt = ds.Tables[0];

        g2.DataSource = dt;
        g2.DataBind();
    }

    protected void g2_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
        {
            //for (int i = 2; i <= 5; i++)
            //{
            //    double money = ShoveEL._Convert.StrToDouble(e.Item.Cells[i].Text, 0);
            //    e.Item.Cells[i].Text = (money == 0) ? "" : money.ToString("N");
            //}

            //e.Item.Attributes["onmouseover"] = "this.name=this.style.backgroundColor;this.style.backgroundColor='MistyRose'";
            //e.Item.Attributes["onmouseout"] = "this.style.backgroundColor=this.name;";
        }
    }

    protected void btnGO_Click(object sender, System.EventArgs e)
    {
        BindData();
        BindData2();
    }

    protected void btnDownload_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=HSZY.doc");
        Response.Charset = "";
        Response.ContentType = "application/HSZY.doc";

        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        g.RenderControl(htmlWrite);
        g2.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
    }

    protected void gPager_PageWillChange(object Sender, Shove.Web.UI.PageChangeEventArgs e)
    {
        BindData2();
    }

    protected void gPager_SortBefore(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
        BindData2();
    }

}